package SellerAgent;

import Ontologies.FruitTreeNurserySystemOntology;
import Ontologies.QueryAction;
import Ontologies.TreeWithPrice;
import jade.content.lang.Codec;
import jade.content.onto.OntologyException;
import jade.content.onto.basic.Result;
import jade.core.Agent;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: jiaka_000
 * Date: 13-5-14
 * Time: 9:58
 * the behaviour to perform query request
 */
public class PerformQueryBehaviour extends OneShotBehaviour {
    /**
     * the message received
     */
    ACLMessage msg;
    /**
     * the query action which extract from message
     */
    QueryAction action;

    /**
     * the reference of seller's selling list
     */
    List<TreeWithPrice> sellingList;

    /**
     * the constructor of PerformQueryBehaviour
     * @param a   agent owner
     * @param msg the message received
     * @param action the query action which extract from message
     * @param sellingList  the reference of seller's selling list
     */
    public PerformQueryBehaviour(Agent a, ACLMessage msg, QueryAction action,List<TreeWithPrice> sellingList) {
        super(a);
        this.msg = msg;
        this.action = action;
        this.sellingList=sellingList;
    }

    @Override
    public void action() {
        try {
            performQueryRequest(msg, action);
        } catch (Codec.CodecException e) {
            e.printStackTrace();
        } catch (OntologyException e) {
            e.printStackTrace();
        }
    }

    /**
     *  this method take the responsibility of performing query request
     * @param msg  the message received
     * @param action   the query action which extract from message
     * @throws Codec.CodecException
     * @throws OntologyException
     */
    private void performQueryRequest(ACLMessage msg, QueryAction action) throws Codec.CodecException, OntologyException {
        System.out.println("performQueryRequest");
        //create a target tree object from action's information
        TreeWithPrice targetTree=new TreeWithPrice();
        targetTree.setCommonName(action.getCommonName());
        targetTree.setLatinName(action.getLatinName());
        targetTree.setCategory("");

        for (TreeWithPrice tree : sellingList) {
            if (FruitTreeNurserySystemOntology.isSameTreeType(targetTree, tree)) {
                //set price
                targetTree.setUnitPrice(tree.getUnitPrice());
                //set available quantity
                //if (targetTree.getQuantity() > tree.getQuantity()) {
                    targetTree.setQuantity(tree.getQuantity());
               // }

                break;
            }
        }

        //send response back to buyer
        Result result = new Result(action, targetTree);
        ACLMessage reply = msg.createReply();
        reply.setPerformative(ACLMessage.INFORM);
        myAgent.getContentManager().fillContent(reply, result);
        myAgent.send(reply);

        System.out.println("performQueryRequest  send response");
    }
}
